package com.xgst.land.activity.check

import android.content.Intent
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.xgst.land.R
import com.xgst.land.bean.PDListBean
import com.xgst.land.databinding.ActivityInventoryListLayoutBinding
import com.xgst.land.viewmodel.InventoryListViewModel

//盘点订单列表
class InventoryOrderListActivity : BaseActivity<ActivityInventoryListLayoutBinding>(), OnItemChildClickListener {

    override fun viewBinding(): ActivityInventoryListLayoutBinding {
        return ActivityInventoryListLayoutBinding.inflate(layoutInflater)
    }

    private val adapter = InventoryAdapter()

    private lateinit var model: InventoryListViewModel
    private var page = 1
    private var sign = "1" //检索条件(0:检索所有数据,1:检索核验中/未核验的数据)

    override fun init() {
        model = ViewModelProvider(this)[InventoryListViewModel::class.java]
        binding.tvDeviceName.text = "一体机"

        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
        adapter.setOnItemChildClickListener(this)

        binding.refreshLayout.setOnLoadMoreListener {
            page++
            iniData()
        }

        //创建盘点计划
        binding.imgRight.setOnClickListener {
            startActivity(BuildInventoryOrderActivity::class.java)
        }

        binding.imgBack.setOnClickListener {
            finish()
        }

        binding.btnPositive.setOnClickListener {
            //盘点记录
            startActivity(InventoryRecordActivity::class.java)
        }

        binding.cbAll.setOnCheckedChangeListener { _, isChecked ->
            sign = if (isChecked) {
                "0"
            } else {
                "1"
            }
            iniData()
        }
    }

    override fun onResume() {
        super.onResume()
        iniData()
    }

    private fun iniData() {
        showProgress()
        model.request("$page",sign) {
            dismissProgress()
            if (page > 1) {
                if (it != null) {
                    adapter.addData(it)
                } else {
                    showToast("暂无更多数据")
                }
            } else {
                adapter.setNewInstance(it)
            }
            binding.tvCheckCount.text = "${adapter.data.size}"
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as PDListBean.DataBean.PageListBean
        when (view.id) {
            R.id.tvDelete -> {
                showProgress()
                model.delete("${item.id}") {
                    dismissProgress()
                    showToast("删除成功")
                    adapter.removeAt(position)
                    binding.tvCheckCount.text = "${adapter.data.size}"
                }
            }
            R.id.tvChange -> {
                //修改
                val intent = Intent(this,ChangeInventoryOrderActivity::class.java)
                intent.putExtra("id",item.id)
                startActivity(intent)
            }
            else -> {
                val intent = Intent(this,InventoryOrderDetailActivity::class.java)
                intent.putExtra("id",item.id)
                startActivity(intent)
            }
        }
    }
}